/**
 * Created by IntelliJ IDEA
 *
 * @author zhengjie
 * @Date:2022/1/8 16:51
 * @Version 1.0
 */
package com.zj.ssm.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.zj.ssm.pojo.domain.Staff;
import com.zj.ssm.pojo.domain.Staffdown;
import com.zj.ssm.pojo.domain.query.StaffQuery;
import com.zj.ssm.pojo.domain.vo.StaffVO;
import com.zj.ssm.result.AjaxResult;
import com.zj.ssm.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("stasource")
public class StaffSourceController {

    @Autowired
    private StaffService staffService;

    @RequestMapping("download")
    public void download(StaffQuery query, HttpServletResponse response) throws Exception{

        AjaxResult ajaxResult = staffService.queryStaff(query);
        Map map = (Map) ajaxResult.getData();
        List<Staff> currentData = (List<Staff>) map.get("currentData");
        System.out.println(currentData);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("员工信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(   response.getOutputStream() , Staff.class   )  .sheet("员工信息").doWrite( currentData  );

    }

    @RequestMapping("upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile hehe) throws Exception {

        ExcelReaderBuilder read = EasyExcel.read(hehe.getInputStream());
        List<Map<String,Object>> objects = read.doReadAllSync();
        for (Map<String, Object> m : objects) {
        String name= (String) m.get(0);
        String address= (String) m.get(1);
        Integer age=Integer.parseInt(m.get(2).toString());
        String education= (String) m.get(3);
        String telephone= (String) m.get(4);
        Integer rid=Integer.parseInt(m.get(5).toString());
        Integer bid=Integer.parseInt(m.get(6).toString());
        Integer kid=Integer.parseInt(m.get(7).toString());

        StaffVO staffVO = new StaffVO();
        staffVO.setName(name);
        staffVO.setAddress(address);
        staffVO.setAge(age);
        staffVO.setEducation(education);
        staffVO.setTelephone(telephone);
        staffVO.setRid(rid);
        staffVO.setBid(bid);
        staffVO.setKid(kid);
        AjaxResult ajaxResult = staffService.addStaff(staffVO);

        }
        return AjaxResult.success();

    }

}
